#include "Precomp.h"
#include "ltPortal.h"
#include "ltPluggableFactory.h"
#include "ltSaveData.h"
#include "ltMapState.h"
#include "ltMapFactory.h"
#include "mapformat.pb.h"

//Lotus::PluggableProduct<Lotus::Objects::Portal> Portal("Portal");

namespace Lotus
{
	namespace Objects
	{
		std::list<SaveData*> Portal::TransferList = std::list<SaveData*>();
		std::string Portal::targetPortalName = std::string("");

		void Portal::onCreate()
		{
		}

		Portal::~Portal()
		{
		}

		void Portal::onMapBegin()
		{
			if(targetPortalName.compare(this->getName()) == 0)
			{
				foreach(SaveData* data, TransferList)
				{
					mapformat::Vector* vector;
					Ogre::Vector3 ogreVector;

					vector = data->innerData->mutable_position();
					ogreVector = this->getPosition();
					vector->set_x(ogreVector.x);
					vector->set_y(ogreVector.y);
					vector->set_z(ogreVector.z);

					vector = data->innerData->mutable_rotation();
					ogreVector = this->getRotation();
					vector->set_x(ogreVector.x);
					vector->set_y(ogreVector.y);
					vector->set_z(ogreVector.z);

					this->getMap()->loadObject(data->innerData, NULL);
				}
				TransferList.clear();
				targetPortalName = "";
			}
		}

		MapState* Portal::transfer(const char* mapFileName, const char* targetPortalName, std::list<MapObject*> tranferList, MapState* needDestroy)
		{
			TransferList.clear();
			SaveData* data = NULL;
			this->targetPortalName = targetPortalName;
			foreach(MapObject* object, tranferList)
			{
				this->getMap()->saveObject(object, data->innerData);
				TransferList.push_back(data);
				this->getMap()->destroyObject(object);
			}
			return MapFactoryBase::getInstance().createMapFromFile(mapFileName);
		}

		void Portal::transfer(MapState* map, const char* targetPortalName, std::list<MapObject*> tranferList)
		{
			Portal* targetPortal = static_cast<Portal*>(map->getObject(targetPortalName));
			SaveData* data = NULL;
			mapformat::Vector* vector;
			Ogre::Vector3 ogreVector;
			foreach(MapObject* object, tranferList)
			{
				this->getMap()->saveObject(object, data->innerData);

				vector = data->innerData->mutable_position();
				ogreVector = targetPortal->getPosition();
				vector->set_x(ogreVector.x);
				vector->set_y(ogreVector.y);
				vector->set_z(ogreVector.z);

				vector = data->innerData->mutable_rotation();
				ogreVector = targetPortal->getRotation();
				vector->set_x(ogreVector.x);
				vector->set_y(ogreVector.y);
				vector->set_z(ogreVector.z);

				map->loadObject(data->innerData, NULL);

				this->getMap()->destroyObject(object);
			}
		}

		void Portal::localTranfer(const char* targetPortalName, std::list<MapObject*> tranferList)
		{
			Portal* targetPortal = static_cast<Portal*>(this->getMap()->getObject(targetPortalName));
			foreach(MapObject* object, tranferList)
			{
				object->setPosition(targetPortal->getPosition());
				object->setRotation(targetPortal->getRotation());
			}
		}
	}
}